package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.AdvimgDto;
import com.itheima.ydd.entity.Advimg;
import com.itheima.ydd.entity.Advpos;
import com.itheima.ydd.service.AdvimgService;
import com.itheima.ydd.service.AdvposService;
import com.itheima.ydd.service.RoleService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RequestMapping("/ydd_advimg")
@RestController
public class AdvimgController {
    @Autowired
    private AdvimgService advimgService;

    @Autowired
    private AdvposService advposService;

    @Autowired
    private RoleService roleService;
    //分页查询
    @GetMapping("list")
    public ResultData list(String advimgdesc ,String advimgpos,int page,int limit){
        Page<Advimg> pageInfo = new Page<>(page,limit);
        LambdaQueryWrapper<Advimg> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(advimgdesc != null,Advimg::getAdvimgdesc,advimgdesc);
        wrapper.like(advimgpos != null,Advimg::getAdvimgpos,advimgpos);
        advimgService.page(pageInfo,wrapper);
        int count = advimgService.count(wrapper);

        Page<AdvimgDto> dtoPage = new Page<>();
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        List<Advimg> advimgList = pageInfo.getRecords();

        List<AdvimgDto> advimgDtos = advimgList.stream().map(item -> {
            AdvimgDto dto = new AdvimgDto();
            BeanUtils.copyProperties(item, dto);

            String advimgpos1 = item.getAdvimgpos();
            Advpos advpos = advposService.getById(advimgpos1);
            dto.setAdvposname(advpos.getAdvposname());
            return dto;

        }).collect(Collectors.toList());
        dtoPage.setRecords(advimgDtos);

        PageResult pageResult = PageResult.init((long)count,(long)limit,(long)page,advimgDtos);

        return ResultData.ok("",pageResult);
    }

    //添加
    @PostMapping("/add")
    public ResultData add(@RequestBody Advimg advimg) {
        advimg.setDefault_data("1");
        advimgService.save(advimg);
        return ResultData.ok("", "成功");
    }

    //删除
    @GetMapping("/del")
    public ResultData del(int id) {//广告图片
        if (roleService.RoleUtils("广告管理","广告图管理","删除")){
            advimgService.removeById(id);
            return ResultData.ok("删除成功", "删除成功");
        }
        return ResultData.error("没有权限无法删除");
    }

    //修改回显
    @GetMapping("/rowInfo")
    public ResultData rowInfo(int id) {
        Advimg advimg = advimgService.getById(id);
        Advpos advpos = advposService.getById(advimg.getAdvimgpos());
        //String advposname = advpos.getAdvposname();
        //将广告位id替换为名字了    然后替换
        advimg.setAdvimgpos(advpos.getAdvposname());

        return ResultData.ok("",advimg );
    }

    //修改
    @PostMapping("/update")
    public ResultData update(@RequestBody Advimg advimg) {
        LambdaQueryWrapper<Advpos> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Advpos::getAdvposname,advimg.getAdvimgpos());
        Advpos advpos = advposService.getOne(lqw);
        if(advpos != null) {
            advimg.setAdvimgpos(advpos.getId() + "");
            advimgService.updateById(advimg);
        }else {
            advimgService.updateById(advimg);
        }
        return ResultData.ok("", "成功");
    }
}